var APP = require("../../../lib/main/app");
var LIST = require("../../../lib/data/HList");
var REFRESH = require("../../../lib/other/refresh");
var Config = require("../../../lib/config/config");
var page = {
	categoryid:'',
	ready: function() {
		var that = this;
		
		// 头部处理
		var headerHeight = $('#header').height();
		$('body').css('padding-top', headerHeight);
		that.categoryid = api.pageParam.id;
		
		try{
			let lang =  api.getGlobalData({key: 'lang'}) || 'cn';
			i18n("[i18n]", {
				lang: lang,// 变更语言
				filePath: "i18n/",
				get: true
			});
		}catch(e){
			//TODO handle the exception
		}
		// 下拉刷新
		REFRESH.refresh('.aui-refresh-content', function(res) {
			if(res.status == "success"){
				LIST.resetData();
			}
			setTimeout(function() {
				REFRESH.cancelLoading(); //隐藏下拉刷新
			}, 4000)
		})
		that.initData();
		
	},
	on: function() {
		var that = this;
		//批量事件start
		var events = [
			//打开页面
			['#add-channel', function() {

			}],
			// 关闭页面
			['#close', function() {
				APP.w.closeWin();
			}],
			
		];
		APP.e.more(events);
		//批量事件end
	},
	methods: {
		//加载数据
		initData: function() {
			var that = this;
			that.getVideo();
		},
		//查询数据
		getVideo: function() {
			var that = this;
			let params = {
				category:that.categoryid,
			}
			var url = 'video/get_list';
			var render = require('./list');
			var renderBox = '#list-box';
			var emptyHtml = '';
			//list展示方式
			LIST.init({
				api: url,
				params: params,
				page:1,
				pageSize:10,//这个是服务器例如默认10个告诉你的，不传则默认是10个，给前端判断用的，只是为了测试请求6个
				// headRefresh:1,
				dataChildKey:'data',
				// debug:'empty',//调试数据为空显示
				// formatKey: 'works',//处理对象
				emptyHtml:emptyHtml,
				render: render,
				renderBox : renderBox,
				renderExtraData:  {imgHost : Config.imgHost},
				isCallback: 'all',
				success: function(res) {
					REFRESH.cancelLoading(); //隐藏下拉刷新
					// console.log('success:' + JSON.stringify(res) );
					// if(res.data.data.length < 10 && res.data.current_page > 1) {
					// 	$(".no-data").removeClass('hidden');
					// }else{
					// 	$(".no-data").addClass('hidden');
					// }
					that.videoPlay();
				}
			});
		},
		videoPlay: function (){
			//只有播放一个 ，其余暂停播放
			var videos = document.getElementsByTagName('video');
			for (var i = videos.length - 1; i >= 0; i--) {
				(function(){
					var p = i;
					videos[p].addEventListener('play',function(){
						pauseAll(p);
					})
				})()
			}
			function pauseAll(index){
				for (var j = videos.length - 1; j >= 0; j--) {
					if (j!=index) videos[j].pause();
				}
			};
		},
	}
}
API.run(page);
